package code.hkgolden.android.service;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;
import code.hkgolden.android.data.Topic;

import com.google.inject.Inject;
import com.google.inject.Singleton;

//@Singleton
//public class TestContentService implements ContentService {
//
//	private ObjectMapper mapper;
//
//	@Inject
//	private HttpService httpService;
//
//	protected final Logger logger = LoggerFactory.getLogger(getClass());
//
//	public TestContentService() {
//		mapper = new ObjectMapper();
//	}
//
//	@Override
//	public Topic replies(URL url) throws IOException {
//		final String jsonData = httpService.getFromUrl(url);
//		if (jsonData == null) {
//			return null;
//		}
//		return mapper.readValue(jsonData, new TypeReference<Topic>() {
//		});
//	}
//
//	@Override
//	public Topic replies(int id, int page) throws IOException {
//		return replies(ContentService.class.getResource("replies.json"));
//	}
//
//	@Override
//	public Collection<Topic> topics(URL url) throws IOException {
//		final String jsonData = httpService.getFromUrl(url);
//		// logger.trace("jsonData = {}", jsonData);
//		if (jsonData == null) {
//			return null;
//		}
//		final JsonNode rootNode = mapper.readValue(jsonData, JsonNode.class);
//		Collection<Topic> result = mapper.readValue(rootNode.get("topic_list")
//				.traverse(), new TypeReference<ArrayList<Topic>>() {
//		});
//		// logger.trace("result = {}", result);
//		return result;
//	}
//
//	@Override
//	public Collection<Topic> topics(String type, int page) throws IOException {
//		return topics(ContentService.class.getResource("topics.json"));
//	}
//
//	private LinkedHashMap<String, String> channelMap = new LinkedHashMap<String, String>();
//
//	@Override
//	public Map<String, String> channels(Context context) throws IOException {
//		if (channelMap.size() > 0) {
//			return channelMap;
//		}
//		InputStream inputStream = null;
//		try {
//			inputStream = context.getAssets().open("channels.xml");
//			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
//					.newInstance();
//			Document document = documentBuilderFactory.newDocumentBuilder()
//					.parse(inputStream);
//			Node selectNode = document.getFirstChild();
//			// System.out.println("selectNode = " + selectNode.getNodeName());
//			NodeList nodeList = selectNode.getChildNodes();
//			for (int i = 0; i < nodeList.getLength(); i++) {
//				Node optionNode = nodeList.item(i);
//				if (optionNode.getNodeType() == Node.ELEMENT_NODE) {
//					// System.out.println("optionNode = "
//					// + optionNode.getNodeName());
//					String value = optionNode.getAttributes().getNamedItem(
//							"value").getNodeValue();
//					// System.out.println("value = " + value);
//					String label = optionNode.getTextContent();
//					// System.out.println("label = " + label);
//					channelMap.put(value, label);
//				}
//			}
//		} catch (Exception e) {
//			throw new RuntimeException(e);
//		} finally {
//			if (inputStream != null) {
//				inputStream.close();
//			}
//		}
//		return channelMap;
//	}
//
//}
